package com.redis.springbootredis.rabbitmq.test;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @create 2020-11-03 19:46
 * 死信队列的生产者
 */
public class ExchangeDlxProducer {

    public static void main(String[] args) throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.238.132");
        connectionFactory.setVirtualHost("ljw");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        Map<String, Object> data = new HashMap<>();
        data.put("ljw","abc");
        data.put("fzt","def");
        AMQP.BasicProperties props = new AMQP.BasicProperties().builder()
                .deliveryMode(2)
                .contentEncoding("utf-8")
                .headers(data)
                .build();
        for(int i = 0 ; i < 10 ; i++){
            channel.basicPublish("exchangetestdlx","routingkeytestdlx",true,props,("dlx"+i).getBytes());
            System.out.println("发送成功"+i);
        }
        System.out.println("发送成功");
        //生产者发送消息后需要释放资源
        channel.close();  //关闭信道
        connection.close(); //关闭连接
    }
}
